package com.ponhu.ea2.goodsCenter.bms.api.utils;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class CustomCellStyleHandler extends AbstractRowWriteHandler {

    private List<Integer> indexList;
    private AtomicInteger atomicInteger;

    public CustomCellStyleHandler(List<Integer> indexList) {
        this.indexList = indexList;
        atomicInteger = new AtomicInteger(0);
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                Row row, Integer relativeRowIndex, Boolean isHead) {

        if (row.getRowNum() == 0) return;

        Cell lastCell = row.getCell(row.getLastCellNum() - 2);
        if (ObjectUtils.isNotEmpty(lastCell) && "货品不存在".equals(lastCell.getStringCellValue())) return;

        for (int i = 0; i < 35; i++) {
            Cell cell = row.getCell(i);
            if (cell == null) continue;
            if (indexList.contains(i)) {
                XSSFCellStyle style = (XSSFCellStyle) cell.getRow().getSheet().getWorkbook().createCellStyle();
                //设置自动换行
                style.setWrapText(true);
                //水平对齐方式
                style.setAlignment(HorizontalAlignment.LEFT);
                //垂直对齐方式
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                cell.setCellStyle(style);
            }
        }
    }

}